<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="公司" prop="office.id"
                :rules="[
                  {required: true, message:'公司不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.office.id" placeholder="请选择"
                  style="width: 100%">
                  <el-option v-for="(item, index) in companys" :label="item.name"
                    :key="index" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
        </el-col>
        
        <el-col :span="12">
            <el-form-item label="合同类型" prop="contracttype"
                :rules="[
                  {required: true, message:'合同类型不能为空', trigger:'blur'}
                 ]">
              <!-- <el-input v-model="inputForm.contracttype" placeholder="请填写合同类型"     ></el-input> -->
              <el-select v-model="inputForm.contracttype" placeholder="请选择"
                  style="width: 100%;">
                  <el-option v-for="item in $dictUtils.getDictList('formwork_type')"
                    :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="是否为默认" prop="isdefault"
                :rules="[
                  {required: true, message:'是否为默认不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.isdefault" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="合同附件" prop="annex"
                :rules="[
                  {required: true, message:'合同附件不能为空', trigger:'blur'}
                 ]">
                 <!-- :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/contractmanagement/backstageContract`" 本该是61行-->
                  <el-upload ref="annex"
                        v-if="visible"
                        :action="`${this.$http.BASE_URL}/dlyrlTest/jobUpload`"
                        :headers="{token: $cookie.get('token')}"
                        :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                        :on-success="(response, file, fileList) => {
                          inputForm.annex = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                        }"
                        :on-remove="(file, fileList) => {
                          $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                            $message.success(data.msg)
                          })
                          inputForm.annex = fileList.map(item => item.url).join('|')
                        }"
                        :before-remove="(file, fileList) => {
                          return $confirm(`确定移除 ${file.name}？`)
                        }"
                        multiple
                        :limit="5"
                        :on-exceed="(files, fileList) =>{
                          $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                        }"
                        :file-list="fileLists">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip" style="color:red">文件内红色字体不能修改！</div>
                      </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        fileLists:[],
        companys:[],
        inputForm: {
          id: '',
          office: {
            id: ''
          },
          annex: '',
          contracttype: '',
          isdefault: '',
          remarks: ''
        }
      }
    },
    components: {
      SelectTree
    },
    activated(){
      this.getCompanys();
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建合同模板`
        } else if (method === 'edit') {
          this.title = '修改合同模板'
        } else if (method === 'view') {
          this.title = '查看合同模板'
        }
        this.fileLists = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/sys/syscontractformwork/sysContractFormwork/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.sysContractFormwork)
              this.inputForm.annex.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.fileLists.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      getCompanys() {
				this.companys = [];
				this.$http({
					url: "/sys/web/userinfo/findCompany",
					method: "get",
					params: {
						id: this.$store.state.user.companyId
					},
				}).then(({
					data
				}) => {
					if (data) {
						this.companys = data.list;
					}
				});
			},
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/sys/syscontractformwork/sysContractFormwork/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>